<?php


if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
 	require_once (APPLICATION_PATH . "\\models\\connectionDb.php");
} else {
    require_once (APPLICATION_PATH . "//models//connectionDb.php");
}


class Application_Model_DbTable_Friends extends Zend_Db_Table_Abstract
{

    protected $_name = 'Friends';
    protected $_primary = 'id';

    public function sendFriendRequest($senderID, $receiverID)
    {
    	$data1 = array(
    		'User_id' => $senderID,
    		'Friend_id' => $receiverID,
    		'Status' => '0',          // 0 - Pending Friend Request
    	);

    	$this->insert($data1);
    }
    
    public function  acceptFriendRequest($senderID, $receiverID)
    {
    	$senderID = (int)$senderID;
    	$receiverID = (int)$receiverID;
    	
    	$data1 = array('Status' => '1'); 	// 1 - Active Friendship
    	$where1 = "User_id = " .$senderID . " AND Friend_id = " . $receiverID;
    	$this->update($data1, $where1);		//Smeni go zapisot od friend request, so status - aktivno prijatelstvo
    	
    	$data2 = array(
    		'User_id' => $receiverID,
    		'Friend_id' => $senderID,
    		'Status' => '1',          // 1 - Active Friendship
    	);
    	$this->insert($data2);			// Dodadi ushte eden zapis po prifakjanje na prijatelot
    }
    
	public function  addSelfAsFriend($User_id) //Kope ti ne si tacan! shto ke ti gu ovaj funkcija???
    {
    	$User_id = (int)$User_id;    	
    	
    	$data2 = array(
    		'User_id' => $User_id,
    		'Friend_id' => $User_id,
    		'Status' => '1',          // 1 - Active Friendship
    	);
    	$this->insert($data2);			// Dodadi se sam sebe, potrebno poradi polesno manipuliranje na podatocite, se pravi pri kreiranje na user
    }
    
    public function checkRequest($user1, $user2) //Proveruva dali vekje ima isprateno baranje za prijatelstvo nekoj od dvata usera. Se koristi za da se zabrani dupliranje na requesti
    {
    	$user1 = (int)$user1;
    	$user2 = (int)$user2;
    	
    	$row1 = $this->fetchRow("User_id = '" . $user1 . "' AND Friend_id = " . $user2);
    	$row2 = $this->fetchRow("User_id = '" . $user2 . "' AND Friend_id = " . $user1);
    	
    	if(!$row1 and !$row2)
    	{
    		return 0;	//ako ne postoi request pomegju niv ili prijatelstvo, vrati 0.
    	}
    	else {
    		return 1;	//ako barem eden od niv pratil request prema drugiot, ili pak ako se prijateli, vrati 1.
    	}
    }

    public function getFriends($userID)
    {
    	$userID = (int)$userID;
    	$rows = $this->fetchAll("User_id = '" . $userID . "' AND Status = '1'");
    	return $rows;
    }
    
    public function getSentPendingRequests($userID)
    {
    	$userID = (int)$userID;
    	$rows = $this->fetchAll("User_id = '" . $userID . "' AND Status = '0'");
    	return $rows;
    }
    
    public function getReceivedPendingRequests($userID)
    {
    	$userID = (int)$userID;
    	$rows = $this->fetchAll("Friend_id = '" . $userID . "' AND Status = '0'");
    	return $rows;
    }
    
    public function removeFriend($user1ID, $user2ID)
    {
    	$where1 = "User_id = " . $user1ID . " AND Friend_id = " . $user2ID;
    	$where2 = "User_id = " . $user2ID . " AND Friend_id = " . $user1ID;
    	
    	$this->delete($where1);
    	$this->delete($where2);
    }
    
    public function rejectRequest($id)
    {
    	//ovaa funkcija se koristi za odbivanje na friend request i za
    	//pravenje cancel na request koj nie sme go ispratile a ne bil prifaten
    	$this->delete("id = " . $id);
    }
	public function getSearchFriends($friendName,$inject_query,$userid)
    {
    	try{
    		
    		$sql ="select distinct Users.*,BasicInfo.FirstName,BasicInfo.LastName";
		    $sql .=" from Users ";
		    $sql .=" left join BasicInfo on BasicInfo.id = Users.BasicInfo_id     ";
		    $sql .=" left join MedicalInfo on MedicalInfo.id = Users.MedicalInfo_id";
		    $sql .=" left join Condition_MedicalInfo on Condition_MedicalInfo.MedicalInfo_id = MedicalInfo.id";
		    $sql .=" left join Conditions on Conditions.id = Condition_MedicalInfo.Condition_id";
		    $sql .=" where 1=1 ";
		    $sql .=" and Users.Username Like '%".$friendName."%'";
		    $sql .=" and Users.id <> ".$userid;
		    $sql .=" and Users.Token=1";
		    $sql .=" ".$inject_query;
		$sql .=" union";
		    $sql .=" select distinct Users.*,BasicInfo.FirstName,BasicInfo.LastName";
		    $sql .=" from Users ";
		    $sql .=" left join BasicInfo on BasicInfo.id = Users.BasicInfo_id     ";
		    $sql .=" left join MedicalInfo on MedicalInfo.id = Users.MedicalInfo_id";
		    $sql .=" left join Condition_MedicalInfo on Condition_MedicalInfo.MedicalInfo_id = MedicalInfo.id";
		    $sql .=" left join Conditions on Conditions.id = Condition_MedicalInfo.Condition_id";
		    $sql .=" where 1=1 ";
		    $sql .=" and CONCAT(BasicInfo.LastName , ' ' , BasicInfo.FirstName) Like '%".$friendName."%'";
		    $sql .=" and Users.id <> ".$userid;
		    $sql .=" and Users.Token=1";
		    $sql .=" ".$inject_query;
		$sql .=" union";
		    $sql .=" select distinct Users.*,BasicInfo.FirstName,BasicInfo.LastName";
		    $sql .=" from Users ";
		    $sql .=" left join BasicInfo on BasicInfo.id = Users.BasicInfo_id     ";
		    $sql .=" left join MedicalInfo on MedicalInfo.id = Users.MedicalInfo_id";
		    $sql .=" left join Condition_MedicalInfo on Condition_MedicalInfo.MedicalInfo_id = MedicalInfo.id";
		    $sql .=" left join Conditions on Conditions.id = Condition_MedicalInfo.Condition_id";
		    $sql .=" where 1=1 ";
		    $sql .=" and CONCAT(BasicInfo.FirstName , ' ' , BasicInfo.LastName) Like '%".$friendName."%'";
		    $sql .=" and Users.id <> ".$userid;
		    $sql .=" and Users.Token=1";
		    $sql .=" ".$inject_query;
    		
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }
    
	public function areFriends($friend_id,$user_id)
    {
    	try{
    		
    		$sql ="select id";
		    $sql .=" from Friends";
		    $sql .=" where 1=1 ";
		    $sql .=" and User_id = ".$user_id;
		    $sql .=" and Friend_id = ".$friend_id;
		    $sql .=" and Status=1 ";		    
		    
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (empty($rows)) {
	   			return 0;
	  		}
	  		else{
	  			return 1;
	  		}
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }
    public function matchMe($userid)
    {

    	try{
	    	$sql ="select distinct Users.*,BasicInfo.FirstName,BasicInfo.LastName";
	    	$sql .=" from Users";
	    	$sql .=" left join BasicInfo on BasicInfo.id = Users.BasicInfo_id";
	    	$sql .=" left join MedicalInfo on MedicalInfo.id = Users.MedicalInfo_id";
	    	$sql .=" left join Condition_MedicalInfo on Condition_MedicalInfo.MedicalInfo_id = MedicalInfo.id";
	    	$sql .=" left join Conditions on Conditions.id = Condition_MedicalInfo.Condition_id";
	    	$sql .=" where 1=1";
	    	$sql .=" and Users.id <>".$userid;
	    	$sql .=" and Conditions.Name ";
	    	$sql .=" IN (select Conditions.Name";
	    	$sql .=" from Users";
	    	$sql .=" left join MedicalInfo on MedicalInfo.id = Users.MedicalInfo_id";
	    	$sql .=" left join Condition_MedicalInfo on Condition_MedicalInfo.MedicalInfo_id = MedicalInfo.id";
	    	$sql .=" left join Conditions on Conditions.id = Condition_MedicalInfo.Condition_id";
	    	$sql .=" where Users.id=".$userid;
	    	$sql .=" )";
	    	
	    	$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
	    	
    	}    
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
   	}
}

